package com.yan.demo;
/*
    自动装箱过程中
    采用的是valueOf的静态方法,当传一个值时如果这个值在[-128,127]之间则返回的是源码IntegerCache静态方法中静态数组Inter[] Cache数组中的对象
    如果传的值在[-128,127]之外的值,则会new一个新的对象返回
 */
public class Demo4 {
    public static void main(String[] args) {
        Integer num1 = 127;
        Integer num2 = 127;
        System.out.println(num1 == num2);
        Integer num3 = 128;
        Integer num4 = 128;
        System.out.println(num3 == num4);

        /*
            public static Integer valueOf(int i) {
                if (i >= IntegerCache.low && i <= IntegerCache.high)
                    return IntegerCache.cache[i + (-IntegerCache.low)];
                return new Integer(i);
                }

            private static class IntegerCache {
                static final int low = -128;
                static final int high;
                static final Integer[] cache;
                static Integer[] archivedCache;

                static {
                    int h = 127;
                    high = h;
                    int size = (high - low) + 1;
                    if (archivedCache == null || size > archivedCache.length) {
                        Integer[] c = new Integer[size];
                        int j = low;
                        for(int i = 0; i < c.length; i++) {
                            c[i] = new Integer(j++);
                        }
                        archivedCache = c;
                    }
                    cache = archivedCache;
                }
            }
         */

    }
}
